package com.ruipengkj.view.memberView;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.Date;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.KeyStroke;

import com.ruipengkj.ApplicationContext;
import com.ruipengkj.bean.TComment;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.util.UUID;
import com.ruipengkj.commons.util.Util;
import com.ruipengkj.component.JLabelJTextAreaComponent;
import com.ruipengkj.component.JLabelJTextFieldComponent;
import com.ruipengkj.component.RPJButtonBlue;
import com.ruipengkj.component.RPJButtonGray;
import com.ruipengkj.service.TCommentService;
import com.ruipengkj.view.base.BaseDialog;

public class MemberAsks extends BaseDialog {
	private static final long serialVersionUID = 1L;
	private JButton okButton;//确定
	private JButton cancel;//取消
	private JLabelJTextFieldComponent messageID = null;//用户名称
	private JLabelJTextFieldComponent title = null;//标题
	private JLabelJTextAreaComponent content = null; //内容
	private JLabelJTextAreaComponent summary = null; //描述
	//private TOrder tOrderEntity;
	private TComment tCommentEntity;
	public boolean isSuccess=false;  //判断是否成功
	private JScrollPane tableJsp;
	private JTable table = null;//订单Table
	//private OrderDetailModel OrderDetailModel;
	//private TBillDetailService tBillDetailService=BuildSpringApplication.getInstance().getTBillDetailServiceImpl();
	//private TCommodityService tCommodityService=BuildSpringApplication.getInstance().getTCommodityServiceImpl();
	//private TOrderService tOrderService=BuildSpringApplication.getInstance().getTOrderServiceImpl();
	private TCommentService tCommentService =BuildSpringApplication.getInstance().getTCommentServiceImpl();
	public MemberAsks(JDialog jd, String title, boolean b) {
		
		super(jd,title,b);
		this.init();
		this.setSize(400, 500);
		this.setLocationRelativeTo(null);
		this.setResizable(false);
	}
	private void init(){
		//OrderDetailModel = new OrderDetailModel();
		//table=uiFactory.createJTable(OrderDetailModel, this, this, 30, JLabel.CENTER);
		//tableJsp=new JScrollPane(table);
		//this.add(tableJsp, BorderLayout.NORTH);
		this.add(this.topPanel(), BorderLayout.CENTER);
		this.initListener();
		//this.getElements();
	}
	//头部面板
	private JPanel topPanel(){
		JPanel main_jp=new JPanel();
		BoxLayout boxLayout=new BoxLayout(main_jp,BoxLayout.Y_AXIS);
		main_jp.setLayout(boxLayout);
		main_jp.setBackground(Util.PANEL_COLOR);
		messageID=new JLabelJTextFieldComponent("用户名：",30);
		title = new JLabelJTextFieldComponent("类型：", 30);
		content=new JLabelJTextAreaComponent("咨询内容：",30,5);
		summary=new JLabelJTextAreaComponent("咨询回复：",30,5);
		messageID.getText().setEditable(false);
		title.getText().setEditable(false);
		content.getArea().setEditable(false);
		JPanel jp1=new JPanel();
		okButton=new RPJButtonBlue("确定");
		cancel = new RPJButtonGray("取消");
		jp1.setOpaque(false);
		jp1.add(okButton);
		jp1.add(cancel);
		
		main_jp.add(messageID);
		main_jp.add(title);
		main_jp.add(content);
		main_jp.add(summary);
		main_jp.add(jp1);
		
		return main_jp;
	}

	private void initListener(){
		okButton.addActionListener(this);
		cancel.addActionListener(this);
		okButton.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_C, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		Date date=new Date();
		String summarys=summary.getArea().getText().trim();
		tCommentEntity.setStatus((byte)10);
		tCommentEntity.setCommentID(tCommentEntity.getCommentID());
		tCommentEntity.setStatus((byte)10);
			if(e.getSource()==okButton){
				 if(summarys.equals("")){
						JOptionPane.showMessageDialog(null, "回复内容不能为空！");
						summary.getArea().requestFocus();
						return;
				}
				//确定
				try {
					tCommentService.modifyService(tCommentEntity);
					TComment tComment = new TComment();
					tComment=tCommentEntity;
					tComment.setMainCommentID(tCommentEntity.getCommentID());
					tComment.setCommentID(UUID.GenUUID15());
					tComment.setTargetID(tCommentEntity.getTUser().getUserID());
					tComment.setContent(summarys);
					tComment.setReturnPoint(0);
					tComment.setHappenTime(date);
					tComment.setStatus((byte)10);
					tCommentService.saveService(tComment);
					//tOrderService.saveOrUpdateService(tOrderEntity);
					JOptionPane.showMessageDialog(null, "回复成功。");
					isSuccess=true;
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					isSuccess=false;
					JOptionPane.showMessageDialog(null, "操作失败！");
					e1.printStackTrace();
				}
				this.dispose();
			}else if(e.getSource() ==cancel){
				this.dispose();
			}
		
	}
//	//数据库中的数据放入list（读取数据）
//	private Runnable queryBillDetail=new Runnable() {
//		@Override
//		public void run() {
//			try {
//				TBillDetail tBillDetail = new TBillDetail();
//				tBillDetail.setBillID(tOrderEntity.getOrderID());
//				tBillDetail.setBillType(ApplicationContext.BILL_BILLTYPE3);
//				List<TBillDetail> billDetailList = tBillDetailService.findService(tBillDetail);
//				if (billDetailList != null &&  billDetailList.size()!= 0) {
//					int length = billDetailList.size();
//					for (int i = 0; i < length; ++i) {
//						//获取商品 以便查看
////						System.out.println(length+"     "+billDetailList.get(i).getCommodityID());
//						billDetailList.get(i).setCommodity(tCommodityService.getByIdService(billDetailList.get(i).getCommodityID()));
//						OrderDetailModel.addBillDetail(billDetailList.get(i));
//					}
//				}
//			} catch (Exception e) {
//				e.printStackTrace();
//			}
//		}
//	};
	public void showDialog(TComment tComment){
		if(tComment!=null){
			tCommentEntity=tComment;
//			SwingUtilities.invokeLater(queryBillDetail);
			messageID.getText().setText(tComment.getTUser().getRealName());
			title.getText().setText(ApplicationContext.Comment_STATUS_MAP().get((byte)tComment.getTargetType()));
			content.getArea().setText(tComment.getContent());
		}
		this.setVisible(true);
	}
}
